<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/5/16
 * Time: 18:51
 */
include '../classes/logincheck.php';
include "../classes/conn.php";
if(islogin()!='teacher_login'){
    die('error');
}

$id=$_POST['id'];

//根据id找到文件并删除
$query1="select attach,answer from homework where Id='{$id}'";
$res=mysql_query($query1);
$file1=array();
while ($row=mysql_fetch_assoc($res)){
    $file1=$row;
}

$query2="select attach from submit where homework='{$id}'";
$res=mysql_query($query2);
$file2=array();
while ($row=mysql_fetch_assoc($res)){
    $file2=$row;
}
if(!empty($file1['attach'])){
    del_file($file1['attach']);
}
if(!empty($file1['answer'])){
    del_file($file1['answer']);
}

if(!empty($file2['attach'])) {
    del_file($file2['attach']);
}
//删除数据
$query="delete from submit where homework='{$id}'";
mysql_query($query);
$query="delete from homework where Id='{$id}'";
mysql_query($query);

echo "1";
//删除文件
function del_file($file){
    $dir=substr($file,8);
    $dir='..'.$dir;
    if(file_exists($dir)){
        if(unlink($dir)){
            return true;
        }
        else return false;
    }
    else return true;
    echo $dir;
}
